#ifndef SERIALCOMMUNICATION_H
#define SERIALCOMMUNICATION_H

#include <QObject>
#include <QSerialPort>
#include <QThread>

enum SerialPortStatus{TxReady, TxBusy, TxNotOpen};
enum SerialOpenResult{OK, HasOpened, OpenError};
typedef uint32_t BuadRate_t;
class SerialCommunication : public QObject
{
    Q_OBJECT
public:
    explicit SerialCommunication(QObject *parent = nullptr);
    ~SerialCommunication();

public:
    SerialPortStatus port_tx_status;

signals:
    void received_data(QStringList);
    void port_opened(SerialOpenResult);
    void port_closed();
    void error_ocurred(QSerialPort::SerialPortError);

public slots:
    // 初始化连接串口
    void init_port(const QString serialLoc, const BuadRate_t buadrate);

    // 关闭串口
    void disconnect_port();

    // 发送数据
    void write_data(const QByteArray);

private slots:
    void preprocess_data();
    void serialErrorHandler(QSerialPort::SerialPortError error);

private:
    void connect_signal_slot();

private:
    QSerialPort* mSerial;
};

#endif // SERIALCOMMUNICATION_H
